var SYNO_RAIDCALC = SYNO_RAIDCALC || {};
SYNO_RAIDCALC.init = function(t, e) {
    new Vue({
        el: "#RAID_calculator .vue",
        data: t,
        methods: {
            openDialog: function() {
                SYNO_WEB.alert($("#raid_type_data").html())
            }
        },
        mounted: function() {
            $(".tiptip").tipTip({
                defaultPosition: "right"
            })
        },
        computed: {
            raid_spaces: function() {
                return t = this.selected_hdds,
                (a = {
                    hdd_sizes: t.map((function(t) {
                        return e.hdds[t]
                    }
                    )).sort((function(t, e) {
                        return e - t
                    }
                    )),
                    totalCount: t.length,
                    systemSize: 4 * t.length,
                    hdCount: {},
                    selectedSizeList: [],
                    totalSize: 0,
                    bays_required: e.mapRaidHardDisk[t.length],
                    spaces: {},
                    space_needed: 0
                }).hdd_sizes.forEach((function(t) {
                    a.hdCount[t] = (a.hdCount[t] || 0) + 1,
                    a.totalSize = a.totalSize + t,
                    -1 == a.selectedSizeList.indexOf(t) && a.selectedSizeList.push(t)
                }
                )),
                e.raids.forEach((function(t) {
                    var e = {
                        available: 0,
                        protection: 0,
                        unused: 0,
                        total: 0
                    };
                    switch (t) {
                    case "SHR":
                        if (1 == a.totalCount)
                            e.total = e.available = a.totalSize - a.systemSize;
                        else {
                            var i = a.hdd_sizes[0]
                              , o = a.hdd_sizes[1];
                            e.protection = o - 4,
                            e.unused = i - o,
                            e.available = a.totalSize - e.protection - e.unused - a.systemSize,
                            e.total = e.available + e.protection + e.unused
                        }
                        break;
                    case "SHR-2":
                        if (4 <= a.totalCount)
                            if (i = a.selectedSizeList[0],
                            4 <= a.hdCount[i])
                                e.protection = 2 * (i - 4),
                                e.available = a.totalSize - e.protection - a.systemSize,
                                e.total = e.available + e.protection;
                            else {
                                o = a.selectedSizeList[1] || 0;
                                var n = a.selectedSizeList[2] || 0
                                  , s = a.selectedSizeList[3] || 0;
                                e.protection = 2 * (o - 4),
                                e.unused = (i - o) * a.hdCount[i],
                                4 > a.hdCount[i] + a.hdCount[o] && (e.protection = 2 * (n - 4),
                                e.unused += (o - n) * (a.hdCount[i] + a.hdCount[o])),
                                4 > a.hdCount[i] + a.hdCount[o] + a.hdCount[n] && (e.protection = 2 * (s - 4),
                                e.unused += (n - s) * (a.hdCount[i] + a.hdCount[o] + a.hdCount[n])),
                                e.available = a.totalSize - e.protection - e.unused - a.systemSize,
                                e.total = e.available + e.protection + e.unused
                            }
                        break;
                    case "RAID 0":
                        2 <= a.totalCount && (e.total = e.available = a.totalSize - a.systemSize);
                        break;
                    case "RAID 1":
                        if (2 <= a.totalCount) {
                            var l = a.selectedSizeList[a.selectedSizeList.length - 1];
                            e.available = l - 4,
                            e.protection = (l - 4) * (a.totalCount - 1),
                            e.unused = a.totalSize - l * a.totalCount,
                            e.total = e.available + e.protection + e.unused
                        }
                        break;
                    case "RAID 5":
                    case "RAID F1":
                        3 <= a.totalCount && (l = a.selectedSizeList[a.selectedSizeList.length - 1],
                        e.available = (l - 4) * (a.totalCount - 1),
                        e.protection = l - 4,
                        e.unused = a.totalSize - l * a.totalCount,
                        e.total = e.available + e.protection + e.unused);
                        break;
                    case "RAID 6":
                        4 <= a.totalCount && (l = a.selectedSizeList[a.selectedSizeList.length - 1],
                        e.available = (l - 4) * (a.totalCount - 2),
                        e.protection = 2 * (l - 4),
                        e.unused = a.totalSize - l * a.totalCount,
                        e.total = e.available + e.protection + e.unused);
                        break;
                    case "RAID 10":
                        4 <= a.totalCount && 0 == a.totalCount % 2 && (l = a.selectedSizeList[a.selectedSizeList.length - 1],
                        e.available = e.protection = (l - 4) * (a.totalCount / 2),
                        e.unused = a.totalSize - l * a.totalCount,
                        e.total = e.available + e.protection + e.unused)
                    }
                    null != e && (a.space_needed = Math.max(e.available, a.space_needed)),
                    a.spaces[t] = e
                }
                )),
                a;
                var t, a
            },
            current_items: function() {
                var t = this.ctx.mapRaidHardDisk[this.selected_hdds.length];
                return this.ctx.items[t]
            },
            placeholder_hdds: function() {
                for (var t = [], e = 12; e > 0; e--)
                    t.push(e);
                return t
            },
            to_compare: function() {
                return this.current_items.filter((function(t) {
                    return t.to_compare
                }
                )).map((function(t) {
                    return t.name
                }
                ))
            },
            getCompareLink: function() {
                return "products/compare/" + this.to_compare.join("/")
            },
            recommend_models: function() {
                var t = {
                    primary: [],
                    ip_san: [],
                    secondary: []
                }
                  , e = this.raid_spaces.selectedSizeList[0]
                  , a = this.raid_spaces.space_needed
                  , i = []
                  , o = this.ctx.modelHddMaps ? this.ctx.modelHddMaps : {}
                  , n = "";
                return this.selected_hdds.forEach((function(t) {
                    n = t.toUpperCase().replace(" ", ""),
                    i.indexOf(n) < 0 && i.push(n)
                }
                )),
                this.current_items.forEach((function(n) {
                    !(Object.keys(o).indexOf(n.name) >= 0) || i.some((function(t) {
                        if (o[n.name].indexOf(t) < 0)
                            return !0
                    }
                    )) || (/^UC/.test(n.name) ? t.ip_san.push(n) : e <= n.maxsize && (a <= n.max_single_volume ? t.primary.push(n) : t.secondary.push(n)))
                }
                )),
                this.$nextTick((function() {
                    $("#main").trigger("toggle_checks")
                }
                )),
                t
            }
        },
        watch: {
            selected_hdds: function() {
                SYNO_WEB.updateQueryString("hdds=" + this.selected_hdds.join("|")),
                $(window).scrollTop() < $("#hdd_area").offset().top && SYNO_WEB.scrollTo($("#hdd_area").offset().top)
            }
        },
        components: {
            "raid-bar": {
                template: "#raid-bar_template",
                props: ["value"],
                data: function() {
                    return {
                        val: this.value,
                        raids: e.raids
                    }
                },
                computed: {
                    space: function() {
                        return this.$parent.raid_spaces.spaces[this.val]
                    },
                    note_style: function() {
                        return 0 == this.space.total ? "width: 100%;" : "width: 0%;"
                    }
                },
                watch: {
                    val: function() {
                        this.$emit("input", this.val)
                    }
                },
                methods: {
                    raid_disabled: function(t) {
                        return t != this.val && -1 != this.$parent.selected_raids.indexOf(t)
                    },
                    text: function(t) {
                        return 0 == this.space.total ? "" : 1e3 <= t ? Math.round(t / 100) / 10 + " TB" : t + " GB"
                    },
                    style: function(t) {
                        return 0 == this.space.total ? "width: 0;" : "width: " + (100 * t / this.space.total).toFixed(2) + "%;"
                    }
                }
            },
            recommend: {
                template: "#recommend_template",
                props: ["item"],
                mounted: function() {
                    $(this.$el).find("label[title]").tipTip({
                        defaultPosition: "top"
                    })
                },
                computed: {
                    show_add_compare: function() {
                        return this.item && /^(FS|HD|SA|RC|RS|DS|EDS)/.test(this.item.name)
                    }
                }
            }
        }
    })
}
;
